package queue;

import org.omg.IOP.TAG_ORB_TYPE;

public class ArrayQueue<T> implements QueueADT<T>{

    private T[] array;
    //指向元素左边
    private int head;
    //指向元素右边
    private int tail;
    private int size;

    public ArrayQueue(int size) {
        this.size = size;
        head=0;
        tail=0;
        array= (T[]) new Object[size];
    }

    @Override
    public boolean enqueue(T t) {
        if (tail==array.length){
            System.out.println("队满");
            return false;
        }
        if (tail==array.length && head!=0){
            //数组前端有空余，整体前移，指针也要前移
            //...todo
        }
        array[tail++]=t;
        return true;
    }

    @Override
    public T dequeue() {
        if (tail==head) System.out.println("队空");
        return array[head++];
    }

    @Override
    public T first() {
        return array[head];
    }

    @Override
    public T last() {
        return array[tail-1];
    }

    @Override
    public void show() {
        for (T t : array) {
            System.out.println(t);
        }
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> queue = new ArrayQueue<>(4);
        queue.enqueue(1);
        queue.enqueue(2);
        queue.enqueue(3);
        System.out.println(queue.dequeue());
        System.out.println(queue.dequeue());
        queue.enqueue(4);
        System.out.println(queue.first());
        System.out.println(queue.last());
        queue.enqueue(4);
    }
}
